using System.Collections.Generic;
using System.Xml;
using System.IO;
using com.tc.assistance.protocol.entity;
using com.tc.assistance.entity.device;
namespace com.tc.assistance.parser{

public class ProtocolStreamer {

	static public string stream(ProtocolMessage pm){
        Stream stream = new MemoryStream();

		XmlTextWriter serializer = new XmlTextWriter(stream, null) ;

			if(pm is Request)
				serializer.WriteStartElement("request");
			else serializer.WriteStartElement("report");

			
				serializer.WriteAttributeString("action", pm.action);
				if(pm.fromComponent != null && !pm.fromComponent.Trim().Equals(""))
					serializer.WriteAttributeString("fromComponent", pm.fromComponent);
				if(pm.toComponent != null && !pm.toComponent.Trim().Equals(""))
					serializer.WriteAttributeString( "toComponent", pm.toComponent);
				if(pm.attributes != null){
				foreach(string key in pm.attributes.Keys)
					serializer.WriteAttributeString(key, pm.attributes[key]);
				}
	
					serializer.WriteEndElement();
	
			serializer.WriteEndDocument();
			StreamReader   sr   =   new   StreamReader(stream); 
            string   all   =   sr.ReadToEnd();
			return all;
		
	}
	static public void streamDevice(Stream outd, GeneralDevice o){

		XmlTextWriter serializer = new XmlTextWriter(outd, null) ;
		
			streamDevice(serializer,o);

		
		
	}
	static public  void streamDevice(XmlTextWriter serializer, GeneralDevice o){
		serializer.WriteStartElement( "generalDevice");
		serializer.WriteAttributeString("id", o.getId());
		serializer.WriteAttributeString("name", o.getName());
		serializer.WriteAttributeString( "fixedType", o.isFixedType() + "");
		if(o.getType() != null)
			serializer.WriteAttributeString("type", o.getType());
		serializer.WriteAttributeString("ownerId", o.getOwnerId());
		serializer.WriteAttributeString( "supportedChannel", o.getSupportedChannel().ToString());
		serializer.WriteStartElement("deviceComponents");
		foreach(DeviceComponent c in o.getComponents().Values){
			serializer.WriteStartElement("deviceComponent");
			serializer.WriteAttributeString( "localId", c.getLocalId());
			serializer.WriteAttributeString("name", c.getName());
			serializer.WriteAttributeString("componentType", c.getComponentType());
			serializer.WriteEndElement();
		}
		serializer.WriteEndElement();
		serializer.WriteEndElement();
	}
	public static void streamSimpleRelation(Stream outd, DeviceRelation o) {

				XmlTextWriter serializer = new XmlTextWriter(outd, null) ;

				serializer.WriteStartElement( "relation");
				serializer.WriteAttributeString( "in", o.ind.ToString());
				serializer.WriteAttributeString("out", o.outd.ToString());
				serializer.WriteEndElement();
		
			
		
	}
	public static void streamDetailedRelation(Stream outd, DetailedRelation relation) {
						XmlTextWriter serializer = new XmlTextWriter(outd, null) ;

			serializer.WriteStartElement( "detailedRelation");
			serializer.WriteStartElement("in");
			foreach(Relationship r in relation.ind){
				serializer.WriteStartElement( "relationship");
				serializer.WriteAttributeString("fromId", r.getFromId());
				serializer.WriteAttributeString( "toId", r.getToId());
				serializer.WriteAttributeString( "name", r.getName()==null?"":r.getName());
				serializer.WriteAttributeString( "reverseName", r.getReverseName()==null?"":r.getReverseName());
				serializer.WriteEndElement();
			}
			serializer.WriteEndElement();
			serializer.WriteStartElement( "out");
			foreach(Relationship r in relation.outd){
				serializer.WriteStartElement( "relationship");
				serializer.WriteAttributeString( "fromId", r.getFromId());
				serializer.WriteAttributeString( "toId", r.getToId());
				serializer.WriteAttributeString( "name",  r.getName()==null?"":r.getName());
				serializer.WriteAttributeString("reverseName", r.getReverseName()==null?"":r.getReverseName());
				if(r.getDevice() != null){
	
						streamDevice(serializer, r.getDevice());
		
				}
				serializer.WriteEndElement();
			}
			serializer.WriteEndElement();
			serializer.WriteEndElement();

		
		}
	public static void streamDeviceStatus(Stream outd, GeneralDevice device) {
	
		XmlTextWriter serializer = new XmlTextWriter(outd, null) ;
				serializer.WriteStartElement( "deviceStatus");
				foreach(StatusItem i in device.getStatus().getValues()){
					serializer.WriteStartElement( "statusItem");
					serializer.WriteAttributeString( "localId", i.getLocalId());
					serializer.WriteAttributeString( "status", i.getStatus()==null?"":i.getStatus());
					serializer.WriteEndElement();
				}
				serializer.WriteEndElement();
				
		}
	
	
}
}
